package com.css.zfzx.fxpg.modules.preestimate.workspace.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.common.utils.ProjectTransUtil;
import com.css.zfzx.fxpg.common.utils.VectorDataUtil;
import com.css.zfzx.fxpg.modules.preestimate.workspace.repository.entity.FxPreestimateWorkspaceEntity;
import com.css.zfzx.fxpg.modules.preestimate.workspace.service.FxPreestimateWorkspaceService;
import com.css.zfzx.fxpg.modules.preestimate.workspace.viewobjects.FxPreestimateWorkspaceQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fxpg.modules.vulnerability.province.viewobjects.FxVulnerabilityProvinceQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * @author andy
 * @date 2020-11-19
 */
@Slf4j
@RestController
@RequestMapping("/fxPreestimateWorkspaces")
public class FxPreestimateWorkspaceController {
    @Autowired
    private FxPreestimateWorkspaceService fxPreestimateWorkspaceService;

    @GetMapping("/queryFxPreestimateWorkspaces")
    public RestResponse queryFxPreestimateWorkspaces(HttpServletRequest request, FxPreestimateWorkspaceQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxPreestimateWorkspaceService.queryFxPreestimateWorkspaces(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxPreestimateWorkspace(@PathVariable String id) {
        RestResponse response = null;
        try{
            FxPreestimateWorkspaceEntity fxPreestimateWorkspace = fxPreestimateWorkspaceService.getFxPreestimateWorkspace(id);
            response = RestResponse.succeed(fxPreestimateWorkspace);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveFxPreestimateWorkspace(@RequestBody FxPreestimateWorkspaceEntity fxPreestimateWorkspace) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPreestimateWorkspaceService.saveFxPreestimateWorkspace(fxPreestimateWorkspace);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateFxPreestimateWorkspace(@RequestBody FxPreestimateWorkspaceEntity fxPreestimateWorkspace)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPreestimateWorkspaceService.updateFxPreestimateWorkspace(fxPreestimateWorkspace);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @DeleteMapping
    public RestResponse deleteFxPreestimateWorkspaces(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPreestimateWorkspaceService.deleteFxPreestimateWorkspaces(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxPreestimateWorkspaceService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    @GetMapping("/getFxPreestimateWorkspaces")
    public RestResponse getFxPreestimateWorkspace(HttpServletRequest request, FxPreestimateWorkspaceQueryParams queryParams) {
        RestResponse response = null;
        try{
            List<FxPreestimateWorkspaceEntity> fxPreestimateWorkspaces = fxPreestimateWorkspaceService.getFxPreestimateWorkspaces(queryParams);
            for (FxPreestimateWorkspaceEntity fPWEntity : fxPreestimateWorkspaces) {
                fPWEntity.setCoordinates(ProjectTransUtil.parseWktPolygon(fPWEntity.getCoordinates()));
            }
            response = RestResponse.succeed(fxPreestimateWorkspaces);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/importsss")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = fxPreestimateWorkspaceService.importExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导出
     *
     * @param queryParams
     * @param response
     * @return
     */
    @GetMapping("/export")
    public RestResponse exportExcel(FxPreestimateWorkspaceQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxPreestimateWorkspaceService.exportExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导出失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return null;
    }


    @PostMapping("/import")
    public RestResponse importSHP(@RequestParam("files") MultipartFile[] files) {
        RestResponse restResponse = null;
        try {
            /*InputStream in = file.getInputStream();
            File toFile = new File(file.getOriginalFilename());
            VectorDataUtil.inputStreamToFile(in, toFile, file.getBytes().length);
            String s = fxPreestimateWorkspaceService.importSHPFile(toFile);
            */
            String s = fxPreestimateWorkspaceService.importSHP(files);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

}